package cn.com.asb.mdms.common.util;

import java.sql.Timestamp;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import cn.com.asb.mdms.common.log.MyLogger;

public class StringUtil {
	protected static Logger log = MyLogger.getLog(StringUtil.class.getName());

	static public boolean isInArray(String[] array, String element) {
		for (int i = 0; i < array.length; i++) {
			String skipProp = array[i];
			if (skipProp.equals(element)) {
				return true;
			}
		}
		return false;
	}
	

	/**
	 * when params is {["uuid","911"],["ppp","pp1"]}
	 * path is /csp/aaa.do?uuid={uuid}#ppp={ppp}
	 * after replaceParams4Path 
	 * should return /csp/aaa.do?uuid=991&ppp=pp1
	 * @param path
	 * @param params
	 * @return
	 * @throws Exception 
	 */
	static public String replaceParams4UrlPath(String path, Map params) throws Exception {
		StringBuilder sb = new StringBuilder();
		log.debug("===========  old path : 	" + path);
		String tempPath = path.replaceAll("#", "&") ;
		log.debug("===========  tempPath path : 	" + path);
		String baseUrl = tempPath .substring(0,tempPath .indexOf("?"));
		sb.append(baseUrl);
		String paramsUrl = tempPath .substring(tempPath .indexOf("?"));
		int indexOfOpenBracket = paramsUrl.indexOf("{");
		int indexOfCloseBracket = paramsUrl.indexOf("}");
		while(indexOfOpenBracket!=-1){
			sb.append(paramsUrl.substring(0,indexOfOpenBracket));
			String key = paramsUrl.substring(indexOfOpenBracket+1,indexOfCloseBracket);
			if(params.containsKey(key)){
				sb.append(params.get(key));
			}else{
				throw new Exception("Un expected key in params :" + key);
			}
			paramsUrl = paramsUrl.substring(indexOfCloseBracket+1);
			indexOfOpenBracket = paramsUrl.indexOf("{");
			indexOfCloseBracket = paramsUrl.indexOf("}");
		}
		
		return sb.toString();
	}
	
	static public boolean isNotNullObject(Object o){
		if(o != null ){
			if (o instanceof String) {
				if(StringUtils.isNotBlank((String)o))
					return true;
				else 
					return false;
			}
			return true;
		}
		
		return false;
	}

	public static void main(String[] args) {
		String s = "";
		Object o = new Integer(0);
		long currTime = System.currentTimeMillis();
		Timestamp ts = new Timestamp(currTime);
		System.out.println(StringUtil.isNotNullObject(s));
		System.out.println(StringUtil.isNotNullObject(o));
		System.out.println(StringUtil.isNotNullObject(null));
		System.out.println(StringUtil.isNotNullObject(ts));
		
	}


	/*
	 * add by xmx @20070920
	 */
	public static boolean isBlank(String id) {
		return !isNotNullObject(id);
	}

}